package com.hq.fxh.No3_TuiGuang.Activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.AppCompatRadioButton;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;


import com.hq.fxh.HttpApi.UserServiceImpl;
import com.hq.fxh.Intefers.NetworkSubscriber;
import com.hq.fxh.Modle.GetPushDataRes;
import com.hq.fxh.No0_ChengQu.Activity.BaseActivity;
import com.hq.fxh.No3_TuiGuang.Adaoter.RGStaticsAdapter;
import com.hq.fxh.No3_TuiGuang.Fragment.PushStatisticDataFragment;
import com.hq.fxh.No3_TuiGuang.MyMoBan.Activity.item.ToastUtils;
import com.hq.fxh.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


import cn.jiguang.analytics.android.api.JAnalyticsInterface;


public class PushStatisticDataActivity extends BaseActivity {

    private int exportOrClick;
    private List<Fragment> list_fragment;                                //定义要装fragment的列表
    private List<String> list_title;                                     //tab名称列表
    private ViewPager vp;
    private TabLayout tab_title;
    private RGStaticsAdapter adapter;
    private ImageView back;
    private PushStatisticDataFragment exportFragment;
    private PushStatisticDataFragment cllickFragment;
    private int datetype = 1;
    private TextView date;
    private RadioGroup rg_date;
    private AppCompatRadioButton rank_day;
    private AppCompatRadioButton rank_month;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_push_statistic_data;
    }

    @Override
    protected void initView() {
        setSysColor(R.color.red,true);
        vp = (ViewPager) findViewById(R.id.stati_detail_vp);
        tab_title = (TabLayout) findViewById(R.id.stati_detail_tab_title);
        date = (TextView) findViewById(R.id.date);
        rg_date = (RadioGroup) findViewById(R.id.rg_date);
        rank_day = (AppCompatRadioButton) findViewById(R.id.rank_day);
        rank_month = (AppCompatRadioButton) findViewById(R.id.rank_month);
        back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initData() {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        String strDate = formatter.format(curDate);
        date.setText(strDate);

        Bundle bundle = this.getIntent().getExtras();
        exportOrClick = bundle.getInt("position");

        exportFragment = new PushStatisticDataFragment();//曝光
        cllickFragment = new PushStatisticDataFragment();//点击

        list_fragment = new ArrayList<>();
        list_fragment.add(exportFragment);
        list_fragment.add(cllickFragment);

        list_title = new ArrayList<>();
        list_title.add("曝光");
        list_title.add("点击");

        //设置TabLayout的模式
        tab_title.setTabMode(TabLayout.MODE_FIXED);
        tab_title.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return true;
            }
        });
        //为TabLayout添加tab名称
        tab_title.addTab(tab_title.newTab().setText(list_title.get(0)));
        tab_title.addTab(tab_title.newTab().setText(list_title.get(1)));

        getPushData(datetype);
        adapter = new RGStaticsAdapter(this.getSupportFragmentManager(), list_fragment, list_title);
        vp.setAdapter(adapter);
        tab_title.setupWithViewPager(vp);
        vp.setCurrentItem(exportOrClick);
        registenerListener();
    }

    @Override
    protected void onResume() {
        super.onResume();
        JAnalyticsInterface.onPageStart(this,"互推统计数据");
    }

    @Override
    protected void onPause() {
        super.onPause();
        JAnalyticsInterface.onPageEnd(this,"互推统计数据");
    }

    private void registenerListener() {
        rg_date.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rank_day:
                        rank_day.setTextColor(getResources().getColor(R.color.white));
                        rank_month.setTextColor(getResources().getColor(R.color.black5));
                        datetype=1;
                        getPushData(datetype);
                        break;
                    case R.id.rank_month:
                        datetype=2;
                        rank_day.setTextColor(getResources().getColor(R.color.black5));
                        rank_month.setTextColor(getResources().getColor(R.color.white));
                        getPushData(datetype);
                        break;
                }
            }
        });
    }

    private void getPushData(final int datetype) {//获取datetype=1当日或datetype=2当月数据
        UserServiceImpl.getPushData(datetype, new NetworkSubscriber<GetPushDataRes>() {
            @Override
            public int onSuccess(GetPushDataRes data) {
                super.onSuccess(data);
                if (data != null && data.getCode() == 0) {
                    List<GetPushDataRes.DataBean> pushData = data.getData();
                    exportFragment.setData(0,datetype,pushData);
                    cllickFragment.setData(1,datetype,pushData);
                } else {
                    ToastUtils.show(PushStatisticDataActivity.this, data.getMsg());
                }
                return 0;
            }
        });
    }
}
